//
//  SelectedWithIndexView.swift
//  PMS
//
//  Created by yangchao on 16/4/12.
//  Copyright © 2016年 途家. All rights reserved.
//
@objc
public protocol SelectedWithButtonIndexProtocol {
    func selectedWithButtonAndIndex(_ button:UIButton,index:Int)
}

import UIKit

public class SelectedWithIndexView: UIView {
    
    public var buttonTitleArray = [String]()
    public var rowBy : Int?
    public var totalButtonArray = [UIButton]()
    weak public var delegate : SelectedWithButtonIndexProtocol?
    public var defauleSelectedIndex : Int? {
        didSet {
            self.refreshButtonState(nil, selectIndex: defauleSelectedIndex)
        }
    }
    
    public init(frame: CGRect,withTitleButtonArray:[String],withRowBy:Int) {
        super.init(frame: frame)
        self.buttonTitleArray = withTitleButtonArray
        self.rowBy = withRowBy
        self.backgroundColor = CommonColors.c_333333.withAlphaComponent(0.4)
        self.addTap()
        self.prepareContentView()
    }
    
    public func addTap(){
        let tap = UITapGestureRecognizer(target: self, action: #selector(SelectedWithIndexView.hide))
        tap.numberOfTapsRequired = 1
        self.addGestureRecognizer(tap)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func prepareContentView() {
        // the number of rows
        let lines : Int = rowBy!
        let topView = UIView(frame: CGRect(x: 0,y: 0,width: CommonDefine.kScreenWidth,height: 0))
        topView.backgroundColor = CommonColors.c_ffffff.withAlphaComponent(0.9)
        for i in 0..<buttonTitleArray.count {
            let row  = i/rowBy! // 列
            let loc  = i%rowBy! // 行
            let button = UIButton(frame: CGRect(x: CGFloat(loc)*((CommonDefine.kScreenWidth-20)/CGFloat(lines))+20,y: 20+CGFloat(row)*50,width: (CommonDefine.kScreenWidth-20)/CGFloat(lines)-20,height: 40))
            button.addTarget(self, action: #selector(SelectedWithIndexView.withSelectAction(_:)), for: UIControl.Event.touchUpInside)
            button.tag = 1000 + loc + row*rowBy!
            button.layer.borderColor = CommonColors.c_eeeeee.cgColor
            button.layer.borderWidth = 1
            button.setBackgroundColor(CommonColors.c_ffffff, forState: UIControl.State())
            button.setTitle(buttonTitleArray[loc+row*rowBy!], for: UIControl.State())
            button.setTitleColor(CommonColors.c_8c9eff, for: UIControl.State())
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.layer.cornerRadius = 3
            button.layer.masksToBounds = true
            totalButtonArray.append(button)
            topView.addSubview(button)
            topView.frame.size.height = button.frame.maxY+20
        }
        self.addSubview(topView)
    }
    

    @objc public func withSelectAction(_ button:UIButton)  {
        //button.selected = true
        self.delegate?.selectedWithButtonAndIndex(button, index: button.tag-1000)
        self.refreshButtonState(button)
        self.hide()
    }
    

    public func refreshButtonState(_ selectButton : UIButton? = nil,selectIndex:Int? = nil){
        for i in 0..<totalButtonArray.count  {
            if totalButtonArray[i] .isEqual(selectButton) || i == selectIndex {
                totalButtonArray[i].setBackgroundColor(CommonColors.c_8c9eff, forState: UIControl.State())
                totalButtonArray[i].setTitleColor(CommonColors.c_ffffff, for: UIControl.State())
            }else{
                totalButtonArray[i].setBackgroundColor(CommonColors.c_ffffff, forState: UIControl.State())
                totalButtonArray[i].setTitleColor(CommonColors.c_8c9eff, for: UIControl.State())
            }
        }
        self.reloadInputViews()
    }
    //
    @objc public func hide() {
        self.adjustTheOriginY(self.frame.size.height)
    }
    
    public func show() {
        self.adjustTheOriginY(0)
    }
    
    public func adjustTheOriginY(_ _y:CGFloat){
        var y = self.frame.origin.y
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            y = _y
            self.frame.origin.y = y
        })
    }
    
}
public enum BottomBarButtonType : Int,EnumSerializable{
    case defaultType = 0
    case simpleTitleType = 1
    public var serializeValue : AnyObject { return self.rawValue as AnyObject }
    public var copyValue : AnyObject { return self.rawValue as AnyObject }
}

public class GuestAddChoiceView: UIView {
    public var backBlock : ((_ selectIndex:Int)->Void)?
    public init(isOnlyNeedToShowGuest:Bool,backBlock:@escaping ((_ selectIndex:Int)->Void)) {
        super.init(frame: CGRect.zero)
        self.backBlock = backBlock
        translatesAutoresizingMaskIntoConstraints = false
        
        //let width: CGFloat = 30 + 16 + 8 + 56
        let width = 37 + max(NSLocalizedString("扫描证件", comment: "./PMS/Classes/Views/GuestAddChoiceView.swift").stringWidth(14),NSLocalizedString("已有宾客", comment: "./PMS/Classes/Views/GuestAddChoiceView.swift").stringWidth(14)) + 10 + 8
        let height: CGFloat = isOnlyNeedToShowGuest ? 30+16 : 30 + 32 + 22
        
        frame = CGRect(x: 0, y: 0, width: width, height: height)
        let button1 = OpacityFeedbackButton()
        addSubview(button1)
        button1.frame = CGRect(x: 4, y: 4, width: width - 8, height: 16 + 22)
        button1.addTarget(self, action: #selector(GuestAddChoiceView.jumpToScannController), for: .touchUpInside)
        let icon1 = UIImageView(image: UIImage(named: "key_hard_gray"))
        button1.addSubview(icon1)
        icon1.center = CGPoint(x: 15 + 8 - 4, y: 15 + 8 - 4)
        let label1 = UILabel(frame: CGRect(x: 41 - 4, y: 15 - 5 - 2, width: width-10-37, height: 14 + 10))
        button1.addSubview(label1)
        label1.textColor = CommonColors.c_ffffff
        label1.font = UIFont.thinFontWithSize(14)
        label1.text = NSLocalizedString("扫描证件", comment: "./PMS/Classes/Views/GuestAddChoiceView.swift")
        button1.isHidden = isOnlyNeedToShowGuest
        
        let button2 = OpacityFeedbackButton()
        addSubview(button2)
        let originY : CGFloat = isOnlyNeedToShowGuest ? 4 : 15 + 16 + 11
        button2.frame = CGRect(x: 4, y: originY, width: width - 8, height: 16 + 22)
        button2.addTarget(self, action: #selector(GuestAddChoiceView.jumpToGuestController), for: .touchUpInside)
        let icon2 = UIImageView(image: UIImage(named: "check_in_user"))
        button2.addSubview(icon2)
        icon2.center = CGPoint(x: 15 + 8 - 4, y: 11 + 8)
        let label2 = UILabel(frame: CGRect(x: 41 - 4, y: 11 - 3, width: width-10-37, height: 14 + 10))
        button2.addSubview(label2)
        label2.textColor = CommonColors.c_ffffff
        label2.font = UIFont.thinFontWithSize(14)
        label2.text = NSLocalizedString("已有宾客", comment: "./PMS/Classes/Views/GuestAddChoiceView.swift")
        
        let separator = UIView(frame: CGRect(x: 15, y: height / 2, width: width - 30, height: CommonDefine.kPixel))
        addSubview(separator)
        separator.isHidden = isOnlyNeedToShowGuest
        separator.backgroundColor = CommonColors.c_ffffff.withAlphaComponent(0.3)
    }
    
    @objc public func jumpToScannController() {
        if self.backBlock != nil {self.backBlock!(0)}
    }
    
    @objc public func jumpToGuestController() {
        if self.backBlock != nil { self.backBlock!(1)}
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}








